package com.substation.ui.activity;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.greenentity.DataDictionary;
import com.greenentity.Eqpt;
import com.greenentity.Stdby;
import com.greenentity.Substation;
import com.greenentity.Tools;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.net.SubstationNet;
import com.substation.R;
import com.substation.utils.DialogUtils;
import com.substation.utils.GreenDaoManager;
import com.substation.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 变电站档案
 */
public class SubstationRecordActivity extends BaseListActivity {

    SuperAdapter<Substation> adapter;
    List<Substation> datas = new ArrayList<>();

    @Override
    public SuperAdapter getAdapter() {
        adapter = new SuperAdapter<Substation>(R.layout.content_substation_record) {
            @Override
            public void bindView(ViewHolder viewHolder, final Substation s, int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(s.getNames());
                ((TextView) viewHolder.getView(R.id.person)).setText("联系人:" + s.getUsername());
                ((TextView) viewHolder.getView(R.id.phone)).setText("手机:" + s.getPhone());
                ((TextView) viewHolder.getView(R.id.tel)).setText("座机:" + s.getTel());
                ((TextView) viewHolder.getView(R.id.address)).setText("详细地址:" + s.getPosition());
                viewHolder.getView(R.id.call).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DialogUtils.showPhoneDialog(context, s.getPhone(), s.getTel());
                    }
                });
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context,SubstationDetailActivity.class);
                        SPUtils.put(context,"sub_guid",s.getGuid());
                        SPUtils.put(context,"sub_name",s.getNames());
                        startActivity(intent);
                    }
                });
            }
        };
        return adapter;
    }

    @Override
    protected void initView() {
        super.initView();
        datas.addAll(GreenDaoManager.getInstance().getmDaoSession().getSubstationDao().loadAll());
        refresh.setEnabled(false);
        adapter.setData(datas);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_refer,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.refer:
                SubstationNet.getSubtation(context,this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void success(int i, String obj) {
        switch (i){
            case 2:
                JSONObject object=JSON.parseObject(obj);
                String eqptContent=object.getString("restGsubseqptList");
                String substationContent=object.getString("restWsubstationList");
                String dictionarieContent=object.getString("dictionaries");
                String gsubstoolsList=object.getString("gsubstoolsList");//工器具
                String gsubsstandbyList=object.getString("gsubsstandbyList");//备品备件
                List<Eqpt> eqpts=JSON.parseArray(eqptContent,Eqpt.class);
                List<Substation> substations=JSON.parseArray(substationContent,Substation.class);
                List<DataDictionary> dataDictionaries=JSON.parseArray(dictionarieContent,DataDictionary.class);
                List<Tools> toolses=JSON.parseArray(gsubstoolsList,Tools.class);
                List<Stdby> stdbies=JSON.parseArray(gsubsstandbyList,Stdby.class);
                GreenDaoManager.getInstance().getmDaoSession().getSubstationDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getSubstationDao().insertInTx(substations);
                GreenDaoManager.getInstance().getmDaoSession().getEqptDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getEqptDao().insertInTx(eqpts);
                GreenDaoManager.getInstance().getmDaoSession().getDataDictionaryDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getDataDictionaryDao().insertInTx(dataDictionaries);
                GreenDaoManager.getInstance().getmDaoSession().getToolsDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getToolsDao().insertInTx(toolses);
                GreenDaoManager.getInstance().getmDaoSession().getStdbyDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getStdbyDao().insertInTx(stdbies);
                break;
        }
    }
}
